package com.service.socket.process;

import java.util.List;

import org.jboss.netty.channel.Channel;

import com.service.cache.CacheManager;
import com.service.socket.protocol.CmdWord;
import com.service.socket.protocol.Conversion;
import com.service.socket.protocol.Packet;
import com.service.socket.protocol.Resolver;

public class R_Class_F0 extends R_Class_Abstract {

	public R_Class_F0(Channel channel, Packet packet) {
		super(channel, packet);
		this.vehicle = CacheManager.instance().getVehicle(packet.getDeviceNo());
	}

	@Override
	public void execute() {
		//System.out.println("dtu -> server : " + packet.toString());
		int cmdword = Integer.parseInt(packet.cmdWord, 16);
		switch (cmdword) {
		case 0x57:
			method_57();
			break;
		case 0xAA:
			method_AA();
			break;
		case 0xAB:
			method_AB();
			break;
		default:
			break;
		}
	}
	
	private void method_57(){
		List<String> list = Resolver.parseF057(packet.dataArea);
		Packet bean = null;
		try {
			String version = new String(Conversion.hexStringToBytes(list.get(0))).trim();
			int index = Integer.parseInt(Conversion.reversal(list.get(2)), 16);
			bean = PacketCreator.getFrameBean(packet.deviceNo, version, index);
		} catch (Exception e) {
			return;
		}
		SendCommand.send(bean);
		System.out.println("server -> dtu : " + bean.toString());
		//转发给监控器
		SendCommand.sendCtrlDtu(packet);
	}
	
	private void method_AA(){
		if(CmdWord.STATUS_INITIATIVE.equals(packet.status)){
			SendCommand.send(packet);
		}else{
			SendCommand.sendCtrlDtu(packet);
		}
	}
	
	private void method_AB(){
		if(CmdWord.STATUS_INITIATIVE.equals(packet.status)){
			SendCommand.send(packet);
		}else{
			SendCommand.sendCtrlDtu(packet);
		}
	}

}
